<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Ke extends \Phpcmf\Home\Module
{

	public function index() {
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$cid = (int)\Phpcmf\Service::L('input')->get('cid');

		$shuju_data = \Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->get()->getRowArray();
		$quan_data = \Phpcmf\Service::M()->db->table('1_zhishi')->where('id', $cid)->where('fstatus', 1)->get()->getRowArray();
		
		// 内容图片列表
		$tupian_data = dr_get_content_img($shuju_data['neirong']);
		$tupian_list = [];
		foreach ($tupian_data as $k => $v) {
			// 去除&quot;及前面的内容
			$v = preg_replace('/.*&quot;/', '', $v);
			$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
			
		}
		// 头像
		if(dr_avatar($shuju_data['uid'])){
			$touxiang = dr_avatar($shuju_data['uid']);
		} else {
			$touxiang = 1;
		}
		// 九个图 
		$jiugetu_list = [];
		foreach ($shuju_data['tupian'] as $k => $v) {
			if ($k <= 8) {
				$jiugetu_list[] = dr_thumb($v['file'], 250, 250, 0, 'crop');
			}
		}
		// 音频 
		$yinpin_data_0 = dr_string2array($shuju_data['yinpin']);
		$yinpin_data = $yinpin_data_0['file'];

		$yinpin_list = [];
		foreach ($yinpin_data as $k => $v) {
			if ($k <= 8) {
				$yinpin_list[] = dr_get_file($v);
			}
		}
		// 名称
		$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $shuju_data['uid'])->get()->getRowArray();
		$user = dr_member_info($shuju_data['uid']);
		if($userweixin){
			$mingcheng = $userweixin['nickname'];
		} else if($user['name']) {
			$mingcheng = $user['name'];
		} else {
			$mingcheng = $user['username'];
		}
		//更新浏览量
		// 相同的ip 不增加
		// 10小时后才能再次增加浏览量
		$ip = \Phpcmf\Service::L('input')->ip_info();
		$cache_key = 'tiezi_hits_' . $id . '_' . $ip;
		// 10小时 = 36000秒
		if (!\Phpcmf\Service::L('cache')->get_data('zhishi_ke_hits_' . $id.'_'.$ip)) {
			$hits = $shuju_data['hits'] + 1;
			\Phpcmf\Service::M()->db->table('1_zhishi_form_kecheng')->where('id', $id)->update(['hits' => $hits]);
			// 设置缓存，过期时间10小时
			//\Phpcmf\Service::L('cache')->set_data("缓存名称", "缓存变量值，支持数组", "缓存有效期，单位秒");
			\Phpcmf\Service::L('cache')->set_data("zhishi_ke_hits_".$id.'_'.$ip,"ip,1","36000");
		}
		
		
		if($shuju_data['status']==0){
			// 审核中
			if($this->member['is_admin']){
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian_list,
					'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => htmlspecialchars_decode($shuju_data['neirong']),
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'yinpin' => $yinpin_list,
				];

			} else {
				// 审核中
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian_list,
					'jiugetu' => $jiugetu_list,
					'jianjie' => '',
					'neirong_data' => '审核中...',
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					// 使用member数组中的uid，确保有值
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					
				];
			}
			


		} else {
			if($shuju_data['gongkai']==1){
				// 公开
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian_list,
					'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => htmlspecialchars_decode($shuju_data['neirong']),
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'yinpin' => $yinpin_list,
				];
			} else {
				// 隐私
				if($this->member['uid'] == $shuju_data['uid']){
					$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian_list,
					'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => htmlspecialchars_decode($shuju_data['neirong']),
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
				];
				} else {
					$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian_list,
					'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => '隐私内容，仅作者可见',
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
				];

				}
				
			}

			

		}
		


		$this->_json('1', '读取成功', $list_data);
	}

}
